﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace BoneSystem.Extensions.Components
{
    public  static partial class BoneSystemExtensions
    {
        public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, string value,
                Dictionary<string,string> attributes)
        {
            var builder = new TagBuilder("input");
            builder.Attributes["type"] = "text";
            builder.Attributes["name"] = name;
            builder.Attributes["value"] = value;
            builder.Attributes["value"] = value;
            if (attributes != null)
            {
                foreach (var attribute in attributes)
                {
                    builder.Attributes[attribute.Key] = attribute.Value;
                }
            }
            
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
        }

        public static MvcHtmlString BsysTextBoxFor<T, TProperty>(this HtmlHelper<T> htmlHelper, 
            Expression<Func<T, TProperty>> expression)
        {
            var name = ExpressionHelper.GetExpressionText(expression);
            var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var prop = metadata.Model.GetType().GetProperty(name);
            var attributes = prop.CustomAttributes;
            foreach (var attribute in attributes)
            {
                
            }
            return TextBox(htmlHelper, name, metadata.Model.ToString(), new Dictionary<string,string>{});
        }

        public static MvcHtmlString BsysTextBoxFor<T, TProperty>(this HtmlHelper<T> htmlHelper, 
            Expression<Func<T, TProperty>> expression, TextBoxForSettings settings)
        {
            if (settings == null)
            {
                settings = new TextBoxForSettings();
            }
            if (string.IsNullOrEmpty(settings.Name))
            {
                settings.Name = ControlHelper.GetExpressionText(expression);
            }
            var name = settings.Name;
            var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var validators = metadata.GetValidators(htmlHelper.ViewContext);
            var enums = validators.GetEnumerator();
            // Adiciona o controle de foco.
            return TextBox(htmlHelper, name, metadata.Model as string,settings.GetAttributes());
        }

       }
}